﻿using ActivtyUI.App_Code;
using ActivtyUI.Filter;
using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ActivtyUI.Controllers
{
    public class HuaYuanDanController : Controller
    {
        //
        // GET: /HuaYuanDan/

        string CjCountKey = Config.MemcachedKey + "HuaYuanDan_cjCsKey" + System.DateTime.Now.ToString("yyyyMMdd");  //抽奖次数可以
        string jgKey = Config.MemcachedKey + "HuaYuanDan_jgKey";

        weixinUserEntity wxInfo
        {
            get
            {
                weixinUserEntity temp = new weixinUserEntity
                {

                    AppId = "wx25bf1ed19bab0096",
                    AppSecret = "d1067af0669ab1d98ee27adad3f98c5c",
                };
                return temp;
            }

        }


        public ActionResult Index()
        {
            if (System.Configuration.ConfigurationManager.AppSettings["act_hyd_isOpen"] != "1")
            {
                return Content("<h1>活动还没有开启</h1>");
            }
            string openid = Request["openid"];
            string weixinId = Request["weixinId"];
            if (string.IsNullOrEmpty(openid))
            {
                return Content("缺少参数");
            }
            ViewBag.openid = openid;
            ViewBag.weixinId = weixinId;

            if (!string.IsNullOrWhiteSpace(weixinId))
            {
                if (weixinId == "8")
                {
                    ViewBag.SharesURL = "http://mobilehuoke.emoney.cn/Report/NewsReportfbDetail?id=1447";
                }
                else if (weixinId == "15")
                {

                    ViewBag.SharesURL = "http://mobilehuoke.emoney.cn/Report/NewsReportfbDetail?id=1480";
                }
                else if (weixinId == "14")
                {

                    ViewBag.SharesURL = "http://mobilehuoke.emoney.cn/Report/NewsReportfbDetail?id=1482";
                }
                else {
                    ViewBag.SharesURL = string.Empty;
                }
            }
            else
            {
                ViewBag.SharesURL = string.Empty;
            }

            ViewBag.result = CreateResult(openid);
            //long count = CacheManager.RedisCache.Get<long>(CjCountKey + openid);
            //ViewBag.CjCount = count;
            InitSharas(Request.RawUrl);
            return View();
        }

        //产生结果
        public int CreateResult(string openid)
        {
            int result;
            int rn = new Random().Next(1, 1000);
            if (rn <= 400)
            {
                result = 0;  //谢谢
            }
            else
                if (rn > 400 && rn <= 600)
                {
                    result = 1; //一元
                }
                else
                {
                    result = 2;//2800软件
                }
            CacheManager.RedisCache.Set(jgKey + openid, result, 24 * 60);
            return result;
        }

        //转完之后，接收结果
        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult ReceiptResult()
        {
            string openid = Request["openid"];
            string weixinId = Request["weixinId"];
            int cacheResult = CacheManager.RedisCache.Get<int>(jgKey + openid);

            long count = CacheManager.RedisCache.Increment(CjCountKey + openid, 1); //给抽奖次数加1
            if (count > 3 )
            {
                return Content("今天您已用完3次抽奖机会，明天可继续来抽大奖哦！\n本次抽奖无效");
            }
            else
            {
                //如果接收成功，将保存结果的key清掉
                CacheManager.RedisCache.Remove(jgKey + openid);
                //将抽奖记录保存至数据库
                HuaYuanDanBLL.AddChouJiangRec(weixinId, openid, cacheResult);
              
            }

            return Content("");
        }

        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult TiXian()
        {
            string openid = Request["openid"];
            string weixinId = Request["weixinId"];
            string actname = "";
            if (weixinId == "8")
            {
                actname = "HuanYuandanHGP";
            }
            else if (weixinId == "15")
            {
                actname = "HuanYuandanGSZX";
            }
            else if (weixinId == "14")
            {
                actname = "HuanYuandanAGCS";
            }
            decimal money = 0;
            string result = HuaYuanDanBLL.Tixian(weixinId, openid, ActCommon.GetSendRedPackConfigByActName(actname), out money);
            return Content(result);

        }


        private void InitSharas(string rawUrl)
        {
            var timestamp = WeiXinJsSign.getTimestamp();
            var nonceStr = WeiXinJsSign.getNoncestr();
            ViewBag.timestamp = timestamp;
            ViewBag.nonceStr = nonceStr;
            ViewBag.appid = this.wxInfo.AppId;
            ViewBag.appsecret = this.wxInfo.AppSecret;
            string url = Config.Host_Url2 + rawUrl;
            ViewBag.signature = WeiXinJsSign.getWxSign(nonceStr, timestamp, this.wxInfo, url);
        }
    }
}
